package com.yuyou.fn.controller;

import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.platform.permission.DataPermission;
import com.yuyou.fn.settlement.entity.*;
import com.yuyou.fn.settlement.service.*;
import com.yuyou.fn.settlement.vo.*;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Created by wens on 2017/3/13.
 */

@Controller
@RequestMapping("/api/admin/settlement")
public class AdminSettlementController extends ApiBaseController {

    @Resource
    private IMainAccountService mainAccountService ;

    @Resource
    private IFeeService feeService ;

    @Resource
    private IWithdrawalService withdrawalService ;

    @Resource
    private IFeeCategoryService feeCategoryService ;

    @Resource
    private IIncomeCategoryService incomeCategoryService ;

    @ApiOperation("主体账户列表")
    @RequestMapping(value = "/mainAccountList" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Page<MainAccount>> mainAccountList(MainAccountQueryVo mainAccountQueryVo , @RequestParam(value = "pageNo" ,defaultValue = "1") int pageNo, @RequestParam(value = "pageSize" ,defaultValue = "20") int pageSize, HttpServletRequest request ) {
        DataPermission dataPermission = getDataPermission(request);
        Page<MainAccount> page = mainAccountService.findMainAccountByQuery(mainAccountQueryVo ,dataPermission ,pageNo,pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("所有主体账户列表")
    @RequestMapping(value = "/allMainAccountListWithoutStudent" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<List<MainAccount>> allMainAccountListWithoutStudent() {
        List<MainAccount> mainAccountList = mainAccountService.findAllMainAccountListWithoutStudent();
        return ReturnResult.success(mainAccountList);
    }


    @ApiOperation("账户明细报表")
    @RequestMapping(value = "/accountReportDetailList" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Page<ChangeRecord>> accountReportDetailList(AccountReportQueryVo accountReportQueryVo , @RequestParam(value = "pageNo" ,defaultValue = "1") int pageNo, @RequestParam(value = "pageSize" ,defaultValue = "20") int pageSize, HttpServletRequest request ) {
        if(accountReportQueryVo.getEndDate()!= null ){
            accountReportQueryVo.setEndDate(DateUtils.addDays(accountReportQueryVo.getEndDate() , 1 ));
        }
        if(accountReportQueryVo.getStartDate()!= null ){
            accountReportQueryVo.setStartDate(DateUtils.addSeconds(accountReportQueryVo.getStartDate() , -1 ));
        }


        accountReportQueryVo.setDirection(0);
        Page<ChangeRecord> page = mainAccountService.findAccountReportDetailList( accountReportQueryVo, pageNo, pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("账户明细总价")
    @RequestMapping(value = "/accountReportTotalAmount" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Page<ChangeRecord>> accountReportTotalAmount(AccountReportQueryVo accountReportQueryVo ,  HttpServletRequest request ) {
        if(accountReportQueryVo.getEndDate()!= null ){
            accountReportQueryVo.setEndDate(DateUtils.addDays(accountReportQueryVo.getEndDate() , 1 ));
        }
        if(accountReportQueryVo.getStartDate()!= null ){
            accountReportQueryVo.setStartDate(DateUtils.addSeconds(accountReportQueryVo.getStartDate() , -1 ));
        }


        accountReportQueryVo.setDirection(0);
        BigDecimal totalAmount = mainAccountService.accountReportTotalAmount( accountReportQueryVo);
        return ReturnResult.success(totalAmount);
    }




    @ApiOperation("费用列表")
    @RequestMapping(value = "/feeList" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Page<Fee>> feeList(FeeQueryVo feeQueryVo , @RequestParam(value = "pageNo" ,defaultValue = "1") int pageNo, @RequestParam(value = "pageSize" ,defaultValue = "20") int pageSize, HttpServletRequest request ) {
        if(feeQueryVo.getEndDate() != null ){
            feeQueryVo.setEndDate(DateUtils.addDays(feeQueryVo.getEndDate() , 1 ));
        }
        Page<Fee> page = feeService.findFeeByQuery(feeQueryVo, pageNo, pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("费用详情")
    @RequestMapping(value = "/feeDetail" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Fee> feeDetail(@RequestParam("feeId") Long feeId, HttpServletRequest request ) {
        return ReturnResult.success(feeService.findDetail(feeId));
    }

    @ApiOperation("保存费用")
    @RequestMapping(value = "/saveFee" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Page<Fee>> saveFee(FeeSaveVo feeSaveVo) {
        feeService.saveFee( feeSaveVo );
        return ReturnResult.success();
    }

    @ApiOperation("删除费用")
    @RequestMapping(value = "/deleteFee" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Void> deleteFee(@RequestParam("feeId") Long feeId) {
        feeService.deleteFee(feeId);
        return ReturnResult.success();
    }

    @ApiOperation("审核费用")
    @RequestMapping(value = "/auditingFee" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Page<Fee>> auditingFee(@RequestParam("feeId") Long feeId ,@RequestParam("status") Integer status) {
        feeService.auditingFee( feeId , status );
        return ReturnResult.success();
    }


    @ApiOperation("利润明细")
    @RequestMapping(value = "/profitDetail" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Page<ProfitDetailVo>> profitDetail(@RequestParam("mainAccountId") Long mainAccountId , @RequestParam("startDate") Date startDate , @RequestParam("endDate") Date endDate , HttpServletRequest request ) {
        if(endDate != null ){
            endDate = DateUtils.addDays(endDate , 1 );
        }
        if(startDate != null ){
            startDate= TimeUtils.changeSecond(startDate,-1);
        }
        ProfitDetailVo profitDetailVo = mainAccountService.calProfit(mainAccountId, startDate, endDate);
        return ReturnResult.success(profitDetailVo);
    }


    @ApiOperation("账户提现列表")
    @RequestMapping(value = "/accountWithdrawalList" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Page<Withdrawal>> accountWithdrawalList(@RequestParam("mainAccountId") Long mainAccountId, @RequestParam(value = "pageNo" ,defaultValue = "1") int pageNo, @RequestParam(value = "pageSize" ,defaultValue = "20") int pageSize, HttpServletRequest request ) {
        DataPermission dataPermission = getDataPermission(request);
        WithdrawalQueryVo withdrawalQueryVo = new WithdrawalQueryVo();
        withdrawalQueryVo.setMainAccountId(mainAccountId);
        Page<Withdrawal> page  = withdrawalService.findWithdrawalByQuery(withdrawalQueryVo , dataPermission, pageNo,pageSize) ;
        return ReturnResult.success(page);
    }

    @ApiOperation("提现列表")
    @RequestMapping(value = "/withdrawalList" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Page<Withdrawal>> withdrawalList(WithdrawalQueryVo withdrawalQueryVo ,@RequestParam(value = "pageNo" ,defaultValue = "1") int pageNo, @RequestParam(value = "pageSize" ,defaultValue = "20") int pageSize, HttpServletRequest request ) {
        DataPermission dataPermission = getDataPermission(request);
        Page<Withdrawal> page  = withdrawalService.findWithdrawalByQuery(withdrawalQueryVo ,dataPermission , pageNo,pageSize) ;
        return ReturnResult.success(page);
    }

    @ApiOperation("提现申请")
    @RequestMapping(value = "/applyWithdrawal" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Void> applyWithdrawal(@RequestParam("mainAccountId") Long mainAccountId,@RequestParam("amount") BigDecimal amount ,@RequestParam("bankName") String bankName ,@RequestParam("bankCity") String bankCity ,@RequestParam("cardUser") String cardUser, @RequestParam("cardNo") String cardNo, HttpServletRequest request ) {
        withdrawalService.applyWithdrawal(mainAccountId,amount,bankName,bankCity,cardUser,cardNo);
        return ReturnResult.success("OK");
    }

    @ApiOperation("更改提现状态")
    @RequestMapping(value = "/updateWithdrawalStatus" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Void> updateWithdrawalStatus(@RequestParam("withdrawalId") Long withdrawalId ,@RequestParam("status")Integer status, HttpServletRequest request ) {
        withdrawalService.updateWithdrawalStatus(withdrawalId,status);
        return ReturnResult.success("OK");
    }


    @ApiOperation("费用类型列表")
    @RequestMapping(value = "/feeCategoryList" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Page<FeeCategory>> feeCategoryList(@RequestParam(value = "pageNo" ,defaultValue = "1") int pageNo, @RequestParam(value = "pageSize" ,defaultValue = "20") int pageSize, HttpServletRequest request ) {
        Page<FeeCategory> page = feeCategoryService.findFeeCategory(pageNo, pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("所有费用类型")
    @RequestMapping(value = "/allFeeCategory" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Page<FeeCategory>> allFeeCategory() {
        List<FeeCategory> list  = feeCategoryService.findAllFeeCategory();
        return ReturnResult.success(list);
    }

    @ApiOperation("费用类型详情")
    @RequestMapping(value = "/feeCategoryDetail" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<FeeCategoryDetailVo> feeCategoryDetail(@RequestParam("feeCategoryId") Long feeCategoryId, HttpServletRequest request ) {
        return ReturnResult.success(feeCategoryService.findFeeCategoryDetail(feeCategoryId));
    }

/*    @ApiOperation("保存费用类型")
    @RequestMapping(value = "/saveOrUpdateFeeCategory" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Void> saveOrUpdateFeeCategory(@RequestParam(value = "feeCategoryId" , required = false) Long feeCategoryId ,@RequestParam("name") String name ,@RequestParam("remark") String remark ) {
        feeCategoryService.saveOrUpdateFeeCategory( feeCategoryId,name,remark );
        return ReturnResult.success("OK");
    }*/
    @ApiOperation("保存成本类型")
    @RequestMapping(value = "/saveOrUpdateFeeCategory" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Void> saveOrUpdateFeeCategory(FeeCategoryVo feeCategoryVo) {
        feeCategoryService.saveOrUpdate( feeCategoryVo );
        return ReturnResult.success("OK");
    }


    @ApiOperation("删除费用类型")
    @RequestMapping(value = "/deleteFeeCategory" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Void> deleteFeeCategory(@RequestParam("feeCategoryId") Long feeCategoryId) {
        feeCategoryService.deleteFeeCategory(feeCategoryId);
        return ReturnResult.success();
    }

    @ApiOperation("收入类型列表")
    @RequestMapping(value = "/incomeCategoryList" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Page<IncomeCategory>> incomeCategoryList(@RequestParam(value = "pageNo" ,defaultValue = "1") int pageNo, @RequestParam(value = "pageSize" ,defaultValue = "20") int pageSize, HttpServletRequest request ) {
        Page<IncomeCategory> page = incomeCategoryService.findIncomeCategory(pageNo, pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("所有收入类型")
    @RequestMapping(value = "/allIncomeCategory" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Page<IncomeCategory>> allIncomeCategory() {
        List<IncomeCategory> list  = incomeCategoryService.findAllIncomeCategory();
        return ReturnResult.success(list);
    }

    @ApiOperation("费用收入详情")
    @RequestMapping(value = "/incomeCategoryDetail" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<IncomeCategory> incomeCategoryDetail(@RequestParam("incomeCategoryId") Long incomeCategoryId, HttpServletRequest request ) {
        return ReturnResult.success(incomeCategoryService.findById(incomeCategoryId));
    }

    @ApiOperation("保存收入类型")
    @RequestMapping(value = "/saveOrUpdateIncomeCategory" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Page<IncomeCategory>> saveOrUpdateIncomeCategory(@RequestParam(value = "incomeCategoryId" , required = false) Long incomeCategoryId ,@RequestParam("name") String name ,@RequestParam("remark") String remark ) {
        incomeCategoryService.saveOrUpdateIncomeCategory( incomeCategoryId,name,remark );
        return ReturnResult.success();
    }

    @ApiOperation("删除收入类型")
    @RequestMapping(value = "/deleteIncomeCategory" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Void> deleteIncomeCategory(@RequestParam("incomeCategoryId") Long incomeCategoryId) {
        incomeCategoryService.deleteIncomeCategory(incomeCategoryId);
        return ReturnResult.success();
    }

    @ApiOperation("成本类型树形结构")
    @RequestMapping(value = "/feeCategoryTreeMap" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<List<FeeCategoryTreeMapVo> > feeCategoryTreeMap() {
        List<FeeCategoryTreeMapVo> feeCategoryTreeMapVoList=feeCategoryService.findTreeCategoryMapVo();
        return ReturnResult.success(feeCategoryTreeMapVoList);
    }

}
